<template>
  <div class="app-container" ref="container">
    <!-- 固定的搜索和工具栏容器 -->
    <div class="fixed-header">
      <!-- 搜索栏 -->
      <div class="search-bar">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="用户昵称">
            <el-input v-model="formInline.username" placeholder="用户名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleQuery">查 询</el-button>
            <el-button @click="resetQuery">重 置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 工具栏 -->
      <div class="toolbar-right">
        <el-button type="primary" size="small" @click="handleAdd">
          <i class="el-icon-plus"></i> 用户注册
        </el-button>
        <el-button size="small" @click="handleRefresh">
          <i class="el-icon-refresh"></i> 刷新
        </el-button>
      </div>
    </div>

    <!-- 表格内容区域 -->
    <div class="table-container">
      <!-- 表格和分页容器 -->
      <div class="content-container">
        <el-table
            ref="table"
            v-loading="loading"
            :data="UserList"
            border
            style="width: 100%"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="id" label="ID" width="80"></el-table-column>
          <el-table-column prop="username" label="用户名称" min-width="200"></el-table-column>
          <el-table-column prop="usersex" label="用户性别" width="100">
            <template slot-scope="scope">
              <el-tag :type="scope.row.usersex === 1 ? 'success' : 'danger'">
                {{ scope.row.usersex === 1 ? '男' : '女' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="userglod1" label="汪汪币" width="120"></el-table-column>
          <el-table-column prop="userglod2" label="汪汪粮" width="120"></el-table-column>
          <el-table-column label="操作" width="150" fixed="right">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="text"
                  @click="handleEdit(scope.row)"
              >编辑</el-button>
              <el-button
                  size="mini"
                  type="text"
                  @click="handleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 新增和修改 -->

        <!-- 分页 -->
        <div class="pagination-container">
          <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="queryParams.pageNo"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="queryParams.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: 'NovelList',
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      selectedIds: [],
      // 总条数
      total: 0,
      // 小说表格数据
      UserList: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
      },
      formInline: {},
      categoryList: [],
      rules: {
        name: [
          { required: true, message: '小说名字不能为空', trigger: 'blur' }
        ],
      },
      dialogVisible: false,
      form: {},
      books: [],
    }
  },
  created() {
    this.getList()
    this.chategoryList()
  },
  methods: {
    /** 查询用户列表 */
    getList(){
      this.axios.post(`http://localhost:9999/api-user/user/finduser?pageNo=${this.queryParams.pageNo}&pageSize=${this.queryParams.pageSize}`, this.formInline).then(res => {
        this.UserList = res.data.data.list;
        this.total = res.data.data.total;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      // 重置查询表单
      this.formInline = {}
      // 重置分页参数
      this.queryParams = {
        pageNo: 1,
        pageSize: 10
      }
      // 清空选中项（如果有使用表格多选的话）
      if (this.$refs.table) {
        this.$refs.table.clearSelection()
      }
      // 重新加载数据
      this.getList()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = {}
      this.dialogVisible = true
    },
    submitForm(){
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.axios.post(`http://localhost:9999/api-novel/novel/add`, this.form).then(res => {
            this.$message.success('新增成功')
            this.dialogVisible = false
            this.getList()
          })
        }
      })
    },
    /** 编辑按钮操作 */
    handleEdit(row) {
      this.dialogType = 'edit'
      this.dialogVisible = true
      this.form = Object.assign({}, row)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除小说：' + row.title + '?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.success('删除成功')
        this.getList()
      })
    },
    /** 刷新按钮操作 */
    handleRefresh() {
      this.getList()
    },
    /** 查看小说详情 */
    handleView(row) {
      this.$message.success('查看小说：' + row.title)
    },
    /** 上线操作 */
    handleOnline(row) {
      this.$confirm('是否确认上线该小说?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.success('上线成功')
        this.getList()
      })
    },
    /** 下线操作 */
    handleOffline(row) {
      this.$confirm('是否确认下线该小说?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.success('下线成功')
        this.getList()
      })
    },
    /** 章节列表 */
    handleChapter(row) {
      this.$router.push({path: '/novel/chapter', query: {id: row.id}})
    },
    /** 评论列表 */
    handleComment(row) {
      this.$router.push({path: '/novel/comment', query: {id: row.id}})
    },

    // 分页大小改变
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getList()
    },
    // 当前页改变
    handleCurrentChange(val) {
      this.queryParams.pageNo = val
      this.getList()
    },
  }
}
</script>

<style scoped>
.app-container {
  padding: 0;
  background-color: #fff;
  width: 100%;
  overflow: hidden;
  position: relative;
}

.fixed-header {
  position: fixed;
  top: 50px;  /* 与顶部导航栏对齐 */
  left: 210px;  /* 与侧边栏对齐 */
  right: 0;
  background-color: #fff;
  z-index: 999;
  padding: 15px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
}

.search-bar {
  flex: 1;
}

.toolbar-right {
  margin-left: 20px;
}

.table-container {
  margin-top: 70px;  /* 为固定头部留出空间 */
  padding: 0 20px;
}

.content-container {
  background-color: #fff;
  border-radius: 4px;
  width: 100%;
  overflow-x: hidden;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
  padding: 10px 0;
}

.el-table {
  margin-bottom: 20px;
}

.el-link {
  font-size: 12px;
}

/* 添加全局样式，隐藏滚动条但保持滚动功能 */
::-webkit-scrollbar {
  display: none;
}

/* 调整表单项的间距 */
.demo-form-inline .el-form-item {
  margin-bottom: 0;
  margin-right: 20px;
}

.demo-form-inline .el-form-item:last-child {
  margin-right: 0;
}
</style>